using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJSpeech;
using SJVars;
using SJXmlP.XMLClasses;
using Spacejock;
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using yW5Lib;
using yWriter6.My;

namespace yWriter6
{
	[StandardModule]
	internal sealed class modSubs
	{
		private static bool mvarSpeechAvailable = false;

		private static bool AlreadyTested = false;

		private static bool mvarKeyClickLoaded = false;

		public static bool SpeechAvailable()
		{
			bool result = default(bool);
			try
			{
				if (!AlreadyTested)
				{
					AlreadyTested = true;
					try
					{
						new clsTalker();
						result = true;
					}
					catch (Exception ex)
					{
						ProjectData.SetProjectError(ex);
						Exception ex2 = ex;
						result = false;
						ProjectData.ClearProjectError();
					}
					mvarSpeechAvailable = result;
					return result;
				}
				result = mvarSpeechAvailable;
				return result;
			}
			catch (Exception ex3)
			{
				ProjectData.SetProjectError(ex3);
				Exception ex4 = ex3;
				AlreadyTested = true;
				mvarSpeechAvailable = false;
				clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public static void ReformatRTFPara(ref string RTFText)
		{
			if (RTFText.Contains("\\fi"))
			{
				Regex regex = new Regex("\\\\fi[0-9]+\\\\");
				RTFText = regex.Replace(RTFText, "\\fi80\\");
			}
			if (RTFText.Contains("\\pard\\"))
			{
				Regex regex = new Regex("\\\\pard\\\\");
				RTFText = regex.Replace(RTFText, "\\pard\\qj\\");
			}
			if (RTFText.Contains("\\li"))
			{
				Regex regex = new Regex("\\\\li[0-9]+\\\\");
				RTFText = regex.Replace(RTFText, "\\li0\\");
			}
			if (RTFText.Contains("\\sa"))
			{
				Regex regex = new Regex("\\\\sa[0-9]+\\\\");
				RTFText = regex.Replace(RTFText, "\\sa20\\");
			}
		}

		public static void DisplayImage(ref clsProject cProject, string Filename, ref PictureBox Picture1, ref PictureBox Image2, ref TabPage TabPage5)
		{
			//Discarded unreachable code: IL_012b, IL_0142, IL_014d, IL_015d, IL_01c3, IL_01c5, IL_01d9, IL_01fb
			int num = default(int);
			int num4 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				Image2.Image = null;
				num2 = 3;
				if (Operators.CompareString(clsShared.cFile.FileOnly(Filename), "", TextCompare: false) != 0)
				{
					num2 = 5;
					Filename = Path.Combine(cProject.ImagesFolder, clsShared.cFile.FileOnly(Filename));
					num2 = 6;
					if (clsShared.cFile.FileExists(Filename))
					{
						num2 = 8;
						Picture1.Image = Image.FromFile(Filename);
						num2 = 9;
						float num3 = (float)((double)Picture1.Height / (double)Picture1.Width);
						num2 = 10;
						Image2.Top = 8;
						num2 = 11;
						Image2.Left = 5;
						num2 = 12;
						Image2.Width = TabPage5.Width - 10;
						num2 = 13;
						Image2.Height = (int)Math.Round((float)Image2.Width * num3);
						num2 = 14;
						if (Image2.Height > TabPage5.Height - 16)
						{
							num2 = 15;
							Image2.Height = TabPage5.Height - 16;
							num2 = 16;
							Image2.Width = (int)Math.Round((float)Image2.Height / num3);
						}
						num2 = 17;
						Image2.Image = Picture1.Image;
						num2 = 18;
						Picture1.Image = null;
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_01f9: Could not find block for branch target IL_01c5*/;
			}
			if (num4 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		public static string SelectAndCopyImage(ref clsProject cProject)
		{
			string result = "";
			string text = clsShared.cSettings.sGet("SourceImageFolder", MyProject.Computer.FileSystem.SpecialDirectories.MyDocuments);
			if (!clsShared.cFile.FolderExists(text))
			{
				text = MyProject.Computer.FileSystem.SpecialDirectories.MyDocuments;
			}
			string file = clsShared.cFileSel.GetFile(text, "*.jpg;*.gif;*.bmp;*.png");
			if (Operators.CompareString(file, "", TextCompare: false) != 0)
			{
				string text2 = Path.Combine(cProject.ImagesFolder, clsShared.cFile.FileOnly(file));
				clsShared.cFile.CopyFile(file, text2);
				result = clsShared.cFile.FileOnly(text2);
				clsShared.cSettings.sSet("SourceImageFolder", clsShared.cFile.PathOnly(file));
			}
			return result;
		}

		public static int RTFWordcount(string RTFContent)
		{
			int result = default(int);
			try
			{
				RichTextBox richTextBox = new RichTextBox();
				try
				{
					richTextBox.Rtf = RTFContent;
				}
				catch (Exception ex)
				{
					ProjectData.SetProjectError(ex);
					Exception ex2 = ex;
					richTextBox.Text = RTFContent;
					ProjectData.ClearProjectError();
				}
				result = clsShared.cText.Wordcount(richTextBox.Text, ExcludeComments: true);
				return result;
			}
			catch (Exception ex3)
			{
				ProjectData.SetProjectError(ex3);
				Exception ex4 = ex3;
				clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public static string CreateTagList(ref ListView aList)
		{
			string result = "";
			string text = "";
			try
			{
				int num = aList.Items.Count - 1;
				for (int i = 0; i <= num; i++)
				{
					if (aList.Items[i].Selected)
					{
						text = text + Conversions.ToString(aList.Items[i].Tag) + ":";
					}
				}
				if (Strings.Len(text) > 0)
				{
					text = Strings.Left(text, Strings.Len(text) - 1);
				}
				result = text;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public static ListView WhichListClicked(object sender)
		{
			//Discarded unreachable code: IL_0069, IL_0080, IL_008b, IL_009b, IL_00e5, IL_00e7, IL_00fb, IL_011d
			int num = default(int);
			ListView result = null;
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				if (sender is ToolStripMenuItem)
				{
					num2 = 3;
					ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)sender;
					num2 = 4;
					ContextMenuStrip contextMenuStrip = (ContextMenuStrip)toolStripMenuItem.Owner;
					num2 = 5;
					result = (ListView)contextMenuStrip.SourceControl;
				}
				else
				{
					num2 = 7;
					if (sender is ContextMenuStrip)
					{
						num2 = 8;
						ContextMenuStrip contextMenuStrip2 = (ContextMenuStrip)sender;
						num2 = 9;
						result = (ListView)contextMenuStrip2.SourceControl;
					}
					else
					{
						num2 = 11;
						result = null;
					}
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_011b: Could not find block for branch target IL_00e7*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
			return result;
		}

		public static string RTFWithParsedVars(clsProject cProject, clsScene Scene, modVariables.eFinalExportType FinalExportType)
		{
			string textRTF = Scene.TextRTF;
			clsRTF clsRTF = new clsRTF(ref clsShared.cLog, ref clsShared.cSettings);
			if (!textRTF.Contains("["))
			{
				return textRTF;
			}
			RichTextBox richTextBox = new RichTextBox();
			try
			{
				richTextBox.Rtf = textRTF;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				richTextBox.Text = textRTF;
				ProjectData.ClearProjectError();
			}
			if (richTextBox.Text.Trim().ToLower().StartsWith("[include="))
			{
				string text = richTextBox.Text;
				string TextString = "";
				string startTag = "";
				string endTag = "";
				bool flag = false;
				text = Strings.Mid(text, Strings.InStr(text, "=") + 1);
				text = Strings.Left(text, Strings.InStr(text, "]") - 1);
				if (text.Contains("between="))
				{
					string[] array = Strings.Split(text, ",");
					if (array.Length > 3)
					{
						Interaction.MsgBox("Please don't include extra commas when using the 'between=' keyword");
						return textRTF;
					}
					text = array[0];
					startTag = array[1];
					endTag = array[2];
					startTag = Strings.Replace(startTag, "between=(", "").Trim();
					endTag = endTag.Substring(0, endTag.Length - 2).Trim();
					flag = true;
				}
				if (clsShared.cFile.LoadTextFile(text, ref TextString))
				{
					if (flag)
					{
						TextString = new clsXml().ExtractBetweenTags(TextString, startTag, endTag);
						if ((Operators.CompareString(clsShared.cFile.ExtOnly(text), "html", TextCompare: false) == 0) | (Operators.CompareString(clsShared.cFile.ExtOnly(text), "htm", TextCompare: false) == 0))
						{
							TextString = "<html>\r\n" + TextString;
						}
					}
					textRTF = TextString;
					try
					{
						richTextBox.Rtf = textRTF;
					}
					catch (Exception ex3)
					{
						ProjectData.SetProjectError(ex3);
						Exception ex4 = ex3;
						richTextBox.Text = textRTF;
						ProjectData.ClearProjectError();
					}
					richTextBox.DetectUrls = true;
				}
				else
				{
					Interaction.MsgBox("Include file " + text + " not found. Please fix!");
				}
			}
			string text2 = "";
			Collection collection = new Collection();
			IEnumerator enumerator = default(IEnumerator);
			try
			{
				enumerator = modVariables.cVarsGlobal.GetEnumerator();
				while (enumerator.MoveNext())
				{
					clsVar item = (clsVar)enumerator.Current;
					collection.Add(item);
				}
			}
			finally
			{
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
			}
			IEnumerator enumerator2 = default(IEnumerator);
			try
			{
				enumerator2 = cProject.cVars.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					clsVar item2 = (clsVar)enumerator2.Current;
					collection.Add(item2);
				}
			}
			finally
			{
				if (enumerator2 is IDisposable)
				{
					(enumerator2 as IDisposable).Dispose();
				}
			}
			IEnumerator enumerator3 = default(IEnumerator);
			try
			{
				enumerator3 = collection.GetEnumerator();
				while (enumerator3.MoveNext())
				{
					clsVar clsVar = (clsVar)enumerator3.Current;
					if (!(clsVar.Title.ToLower().StartsWith("epub_") | clsVar.Title.ToLower().StartsWith("mobi_")))
					{
						int num;
						do
						{
							num = richTextBox.Find("[" + clsVar.Title + "]", RichTextBoxFinds.None);
							if (num >= 0)
							{
								text2 = ((Operators.CompareString(clsVar.Tags, "1", TextCompare: false) != 0) ? clsVar.DescText : Conversions.ToString(MatchCond(FinalExportType, clsVar)));
								richTextBox.Select(num, Strings.Len("[" + clsVar.Title + "]"));
								Font selectionFont = richTextBox.SelectionFont;
								HorizontalAlignment selectionAlignment = richTextBox.SelectionAlignment;
								if (!clsVar.Title.ToLower().Contains("rtf"))
								{
									text2 = clsRTF.RtfToTxt(text2);
								}
								if (text2.Length == 0)
								{
									richTextBox.SelectedText = "";
								}
								else if (text2.ToLower().Contains("\\rtf"))
								{
									richTextBox.SelectedRtf = text2;
								}
								else
								{
									richTextBox.SelectedText = text2;
								}
								richTextBox.SelectionAlignment = selectionAlignment;
								try
								{
									richTextBox.SelectionFont = selectionFont;
								}
								catch (Exception ex5)
								{
									ProjectData.SetProjectError(ex5);
									Exception ex6 = ex5;
									ProjectData.ClearProjectError();
								}
							}
						}
						while (num >= 0);
					}
				}
			}
			finally
			{
				if (enumerator3 is IDisposable)
				{
					(enumerator3 as IDisposable).Dispose();
				}
			}
			textRTF = richTextBox.Rtf;
			return Strings.Replace(textRTF, "[SceneTitle]", Scene.Title);
		}

		private static object MatchCond(modVariables.eFinalExportType FinalExportType, clsVar cVar)
		{
			switch (FinalExportType)
			{
			case modVariables.eFinalExportType.Html:
				if (Operators.CompareString(cVar.AKA, "Only when exporting to Html", TextCompare: false) == 0)
				{
					return cVar.DescText;
				}
				return "";
			case modVariables.eFinalExportType.Latex:
				if (Operators.CompareString(cVar.AKA, "Only when exporting to LaTeX", TextCompare: false) == 0)
				{
					return cVar.DescText;
				}
				return "";
			case modVariables.eFinalExportType.Ebook:
				if (Operators.CompareString(cVar.AKA, "Only when exporting to ebook", TextCompare: false) == 0)
				{
					return cVar.DescText;
				}
				return "";
			case modVariables.eFinalExportType.Rtf:
				if (Operators.CompareString(cVar.AKA, "Only when exporting to Rtf", TextCompare: false) == 0)
				{
					return cVar.DescText;
				}
				return "";
			default:
				return cVar.DescText;
			}
		}
	}
}
